<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
* @framework : Codeigniter Framework
*
* @author: fatangel26
*
*/

	class News_model extends CI_Model
	{
	    protected $_table;

		public function __construct()
		{
			parent::__construct();

            $this->load->database();

            $this->_table = 'tbl_news';
		}

        /*Basic Method*/

        public function select($data = array())
        {
            if(empty($data))
            {
                $query = $this->db->get($this->_table);

                if($query->num_rows()>0)
                    return $query->result_object();
            }
            else
            {
                $query = $this->db->get_where($this->_table, $data);

                if($query->num_rows()>0)
                    return $query->row();
            }

            return FALSE;
        }

        public function delete($news_id)
        {
            $query = $this->db->delete($this->_table, array('news_id' => $news_id));

            if($this->db->affected_rows() > 0)
                return TRUE;
            return FALSE;
        }

        public function create($data = array())
        {
            if(!empty($data))
            {
                $data['slug'] = $this->create_unique_slug($data['news_title_slug'],$this->_table,'slug');

                $this->db->set('news_created','NOW()',FALSE);

                $query =  $this->db->insert($this->_table,$data);
            }

            if($this->db->affected_rows()>0)
                return TRUE;
            return FALSE;
        }

        public function edit($news_id, $data = array())
        {
            $data['slug'] = $this->create_unique_slug($data['news_title_slug'],$this->_table,'slug','news_id', $news_id);

            if(isset($data['news_title']))
                $this->db->set('news_title', $data['news_title']);
            if(isset($data['news_title_slug']))
                $this->db->set('news_title_slug', $data['news_title_slug']);
            if(isset($data['news_image']))
                $this->db->set('news_image', $data['news_image']);
            if(isset($data['news_image_thumb']))
                $this->db->set('news_image_thumb', $data['news_image_thumb']);
            if(isset($data['news_content']))
                $this->db->set('news_content', $data['news_content']);
            if(isset($data['news_status']))
                $this->db->set('news_status', $data['news_status']);
            if(isset($data['news_hightlight']))
                $this->db->set('news_hightlight', $data['news_hightlight']);
            if(isset($data['news_pages']))
                $this->db->set('news_pages', $data['news_pages']);
            if(isset($data['pages_slug']))
                $this->db->set('pages_slug', $data['pages_slug']);

            $this->db->set('slug', $data['slug']);
            $this->db->set('news_updated', 'NOW()', FALSE);

            $this->db->where('news_id', $news_id);

            if($this->db->update($this->_table))
                return TRUE;
            return FALSE;
        }

        /*Magic Method*/
        protected function create_unique_slug($string, $table, $field='slug', $key=NULL, $value=NULL)
        {
            $t =& get_instance();
        	$slug = url_title($string);
        	$slug = strtolower($slug);
        	$i = 0;
        	$params = array ();
        	$params[$field] = $slug;


            if($key)
                $params["$key != "] = $value;

        	while ($t->db->where($params)->get($table)->num_rows())
        	{
        	    if (!preg_match ('/-{1}[0-9]+$/', $slug ))
        		    $slug .= '-' . ++$i;
        	    else
        		    $slug = preg_replace ('/[0-9]+$/', ++$i, $slug );

        	    $params [$field] = $slug;
        	}

        	return $slug;
        }//End function create_unique_slug
	}

/* End of file Pages_model.php */
/* Location: ./models/frontend/_model.php */